package com.example.swimmingpool.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.swimmingpool.SwimmingPoolThreadLocal;
import com.example.swimmingpool.entity.user.User;
import com.example.swimmingpool.response.CommonResponse;
import com.example.swimmingpool.service.user.UserService;
import com.example.swimmingpool.vo.user.UserVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/list")
    public CommonResponse<List<User>> listUsers() {
        var data = userService.list();
        return CommonResponse.success(data);
    }

    @PostMapping("/info")
    public CommonResponse<UserVo> getUserInfo() {
        var userId = SwimmingPoolThreadLocal.getUserId();
        var userInfo = userService.getById(userId);
        var userVo = BeanUtil.copyProperties(userInfo, UserVo.class);
        return CommonResponse.success(userVo);
    }

    @PostMapping("/update")
    public CommonResponse<Boolean> updateUserInfo(@RequestBody UserVo vo){
        var userId = SwimmingPoolThreadLocal.getUserId();
        vo.setId(userId);
        var res = userService.updateById(BeanUtil.copyProperties(vo,User.class));
        return CommonResponse.success(res);


    }

}
